package cn.ffcs.uec.post.action;

import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.uec.post.action.bean.UecPostEditBean;
import cn.ffcs.uec.post.model.UecPost;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecPostEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecPostEditBean bean = new UecPostEditBean();
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 添加政治面貌
	 */
	private UecPost uecPost;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 */
	public void onCreate$uecPostEditWindow() {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		
	}

	/**
	 * bindBean
	 */
	public void bindBean() {
		opType = (String) arg.get("opType");
		bean.getUecPostEditExt().getBean().getMonthId().setDisabled(true);
		if ("add".equals(opType)) {
			this.bean.getUecPostEditWindow().setTitle("新增岗位信息");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPostEditWindow().setTitle("查看岗位信息");
			uecPost = (UecPost) arg.get("uecPost");
			if (null != uecPost) {
				PubUtil.fillBeanFromPo(uecPost, bean.getUecPostEditExt().getBean());
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPostEditExt(),true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPostEditWindow().setTitle("编辑岗位信息");
			//bean.getGroupBox1().setVisible(false);
			//bean.getUecPostEditDiv().setHeight("160px");
			uecPost = (UecPost) arg.get("uecPost");
			if (null != uecPost) {
				PubUtil.fillBeanFromPo(uecPost, bean.getUecPostEditExt().getBean());
			}
		}
		  
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		if ("view".equals(opType)) {
			onCancel();
		}
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecPost = UecPost.newInstance();
			PubUtil.fillPoFromBean(bean.getUecPostEditExt().getBean(), uecPost);
			DateFormatter();
			uecPost.setId(null);
			uecPost.setDr(1L);
			uecPost.setCreator(currentName);
			uecPost.setCreationtime(DateUtil.getCurrenDate());
			uecPost.addOnly();
			Events.postEvent("onOK", this.self, uecPost);
			this.onCancel();
			Messagebox.show("新增成功！");
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean.getUecPostEditExt().getBean(), uecPost);
			DateFormatter();
			uecPost.setModifier(currentName);
			uecPost.setModifiedtime(DateUtil.getCurrenDate());
			uecPost.updateOnly();
			Events.postEvent("onOK", this.self, uecPost);
			this.onCancel();
			Messagebox.show("更新成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPostEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		// if (StrUtil.isNullOrEmpty(bean.getEncourName().getValue())) {
		// return "填写奖励名称";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourDate().getValue())) {
		// return "填写奖励日期";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourType().getValue())) {
		// return "填写奖励类型";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourMeas().getValue())) {
		// return "填写奖励措施";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourOrg().getValue())) {
		// return "填写奖励机构";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourRank().getValue())) {
		// return "填写奖励级别";
		// } else if (StrUtil.isNullOrEmpty(bean.getEncourGrade().getValue())) {
		// return "填写奖励档次";
		// }
		return null;
	}
	/**
	 * 一套模型，两套标准：日期类型需要格式化
	 */
	public void DateFormatter() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getUecPostEditExt().getBean().getAbortdate().getValue())) {
			uecPost.setAbortdate(formatter.format(bean.getUecPostEditExt().getBean().getAbortdate()
					.getValue()));
		} 
		if (!StrUtil.isNullOrEmpty(bean.getUecPostEditExt().getBean().getBuilddate().getValue())) {
			uecPost.setBuilddate(formatter.format(bean.getUecPostEditExt().getBean().getBuilddate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getUecPostEditExt().getBean().getHrcanceldate().getValue())) {
			uecPost.setHrcanceldate(formatter.format(bean.getUecPostEditExt().getBean().getHrcanceldate()
					.getValue()));
		} 
	}

}
